//
//  LLIntegralViewController.m
//  LoveLove
//
//  Created by 达迩科技 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLIntegralViewController.h"
#import "LLIntegralListViewController.h"

@interface LLIntegralViewController ()<UIScrollViewDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
@property (copy, nonatomic) NSArray *titlesArr;//标题数组
@property (strong, nonatomic) NSMutableArray *titleBtns;//标题按钮数组
@property (strong, nonatomic) NSMutableArray *attentionChildVCs;//子控制器
@property (strong, nonatomic) LLButton *selectedButton;//选择的按钮
@property (strong, nonatomic) UIScrollView *scrollView;//UIScrollView
@property (strong, nonatomic) UIImageView *lineImage;//下划线
@property (strong, nonatomic) UIView *titlesView;//标题文本
@property (strong, nonatomic) UIView *bgView;//标题文本
@property (strong, nonatomic) UILabel *scoreLabel;//标题文本
@property (strong, nonatomic) UILabel *scoreLabel1;//标题文本
@end

@implementation LLIntegralViewController

- (void)viewWillAppear:(BOOL)animated {
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //TODO: 页面Disappear 启用
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD dismiss];
    
    self.titlesArr = @[@"积分明细",@"兑换记录"];
    
    _attentionChildVCs = [NSMutableArray array];
    for (NSInteger i = 0; i < self.titlesArr.count; i++) {
        LLIntegralListViewController *vc = [LLIntegralListViewController new];
        [_attentionChildVCs addObject:vc];
    }
    
    [self changeUserIdentityStatus];
    [self setUpChildViewController];
    [self setUpScrollView];
    [self setUpTitlesView];
    [self addChildVcView];
    
}

#pragma mark - 切换身份
- (void)changeUserIdentityStatus {
    // 移除subView和childViewControllers
    [self.titlesView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat buttonW = LLScreenWidth / self.titlesArr.count;
    CGFloat buttonH = self.titlesView.height;
    self.scrollView.contentSize = CGSizeMake(LLScreenWidth * self.titlesArr.count, 0);
    for (int i= 0; i < self.titlesArr.count ; i ++) {
        LLButton *titleButton = self.titleBtns[i];
        [titleButton setTitle:self.titlesArr[i] forState:0];
        titleButton.tag = i;
        titleButton.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titlesView addSubview:titleButton];
        LLIntegralListViewController *vc = self.attentionChildVCs[i];
        vc.type = i;
        [self addChildViewController:vc];
    }
    
    LLIntegralListViewController *childVc = self.childViewControllers[0];
    childVc.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVc.view];
    [self titleClick:self.titleBtns[0]];
}

#pragma mark - 设置标题view
-(void)setUpTitlesView {
    
    [self bgView];
    
    UIView *titleView123 = [[UIView alloc] initWithFrame:CGRectMake(9, 228, LLScreenWidth-18, 53)];
    titleView123.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView123];
    self.titlesView = titleView123;
    UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.titlesView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(7.5, 7.5)];
    CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
    cornerRadiusLayer.frame = self.titlesView.bounds;
    cornerRadiusLayer.path = cornerRadiusPath.CGPath;
    self.titlesView.layer.mask = cornerRadiusLayer;
    
    UIView *lineview123 = [[UIView alloc] initWithFrame:CGRectMake(9, 52+228, LLScreenWidth-18, 1)];
    lineview123.backgroundColor = LLColorRGB(239);
    [self.view addSubview:lineview123];
    
    NSArray *titlesArr = self.titlesArr;
    CGFloat buttonW = LLScreenWidth / titlesArr.count;
    CGFloat buttonH = titleView123.height;
    for (int i= 0; i < titlesArr.count ; i ++) {
        LLButton *titleButton = self.titleBtns[i];
        [titleButton setTitle:titlesArr[i] forState:0];
        titleButton.tag = i;
        titleButton.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView123 addSubview:titleButton];
    }
    LLButton *button = titleView123.subviews.firstObject;
    [button.titleLabel sizeToFit];
    button.selected = YES;
    self.selectedButton = button;
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(titleView123.width/self.titlesArr.count/2-50/2, titleView123.height - 3, 50, 3)];
    imageview.image = [UIImage imageNamed:@"矩形 4 拷贝 5"];
    [self.titlesView addSubview:imageview];
    self.lineImage = imageview;
}

#pragma mark - 设置scrollView
-(void)setUpScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 280, LLScreenWidth, LLScreenHeight-280)];
    scrollView.backgroundColor = LLColorRGB(243);
    [self.view addSubview:scrollView];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    NSInteger count = [self.childViewControllers count];
    scrollView.contentSize = CGSizeMake(self.view.width * count, 0);
    scrollView.delegate = self;
    self.scrollView = scrollView;
}

-(void)setUpChildViewController {
    for (NSInteger i = 0; i < _attentionChildVCs.count; i++) {
        LLIntegralListViewController *vc = _attentionChildVCs[i];
        vc.type = i;
        [self addChildViewController:vc];
    }
}

#pragma mark - 添加子控制器
-(void)addChildVcView {
    int index = self.scrollView.contentOffset.x / self.scrollView.width;
    LLIntegralListViewController *childVc = self.childViewControllers[index];
    childVc.view.frame = self.scrollView.bounds;
    childVc.type = index;
    [self.scrollView addSubview:childVc.view];
    
}

#pragma mark ------------ UIScrollViewDelegate
// 点击titleButton滑动结束时，一定要调用[setcontentoffset animated ] 或者 [scrollerRactVisible animaated]方法让scroll产生滚动动画，动画结束时才会调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildVcView];
    [LLNotificationCenter postNotificationName:@"EndScrollingAnimationNoti" object:nil];
}

#pragma mark - 滑动scrollview后减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 选中 点击对应的按钮
    int index = scrollView.contentOffset.x / scrollView.width;
    // 添加子控制器
    [self addChildVcView];
    LLButton *button = self.titlesView.subviews[index];
    [self titleClick:button];
    [LLNotificationCenter postNotificationName:@"EndScrollingAnimationNoti" object:nil];
}

#pragma mark - 标题button点击事件
- (void)titleClick:(LLButton *)button {
    // 某个标题按钮被重复点击了
    if (button == self.selectedButton) {
        [LLNotificationCenter postNotificationName:@"TitleButtonDidRepeatClickNotification" object:nil];
    }
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineImage.width = button.titleLabel.width ;
        self.lineImage.centerX = button.centerX;
    }];
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = button.tag * self.view.width;
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark ------ 懒加载
- (NSMutableArray *)titleBtns {
    if (_titleBtns == nil) {
        _titleBtns = [NSMutableArray array];
        for (NSInteger i = 0; i < self.titlesArr.count; i++) {
            LLButton *btn = [LLButton new];
            [btn setTitleColor:LLColorRGB(1) forState:UIControlStateSelected];
            [btn setTitleColor:LLColorRGB(34) forState:0];
            btn.titleLabel.font = LLRegularFont(14);
            [_titleBtns addObject:btn];
        }
    }
    return _titleBtns;
}

#pragma mark ---- dealloc
- (void)dealloc {
    [LLNotificationCenter removeObserver:self];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}


-(UIView *)bgView{
    if (_bgView == nil) {
        _bgView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 34+246)];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_bgView];
        
        UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 246)];
        bgImage.image = [UIImage imageNamed:@"课程图片"];
        [_bgView addSubview:bgImage];
        
        LLButton *backButton = [LLInitBaseViewTool buttonWithTitle:nil image:@"左箭头" titleColor:nil font:0];
        backButton.tag = 0;
        backButton.frame = CGRectMake(9, 30, 30, 30);
        [backButton addTarget:self action:@selector(didSelecttype:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:backButton];
        
        UILabel *titleLabel = [LLInitBaseViewTool labelWithText:@"我的积分" textColor:LLWhiteColor font:18 textAlignment:NSTextAlignmentCenter];
        titleLabel.frame = CGRectMake(LLScreenWidth/2-50, 30+6, 100, 18);
        [_bgView addSubview:titleLabel];
        
        LLButton *guizeButton = [LLInitBaseViewTool buttonWithTitle:@"规则" image:nil titleColor:LLWhiteColor font:15];
        guizeButton.tag = 1;
        guizeButton.frame = CGRectMake(LLScreenWidth-59, 30+7.5, 50, 15);
        [guizeButton addTarget:self action:@selector(didSelecttype:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:guizeButton];
        
        UILabel *title1Label = [LLInitBaseViewTool labelWithText:@"可用积分" textColor:LLWhiteColor font:13 textAlignment:NSTextAlignmentLeft];
        title1Label.frame = CGRectMake(34, 108, 100, 13);
        [_bgView addSubview:title1Label];
        
        UILabel *scoreLabel = [LLInitBaseViewTool labelWithText:@"8888" textColor:LLWhiteColor font:36 textAlignment:NSTextAlignmentLeft];
        scoreLabel.frame = CGRectMake(34, 131, LLScreenWidth-68, 36);
        [_bgView addSubview:scoreLabel];
        
        UILabel *scoreLabel1 = [LLInitBaseViewTool labelWithText:@"累计积分 12026" textColor:LLWhiteColor font:13 textAlignment:NSTextAlignmentLeft];
        scoreLabel1.frame = CGRectMake(34, 186, LLScreenWidth-168-10, 13);
        [_bgView addSubview:scoreLabel1];
        
        
        LLButton *duihaunButton = [LLInitBaseViewTool buttonWithTitle:@"兑换" image:nil titleColor:LLColor(221, 0, 0) font:15];
        duihaunButton.tag = 2;
        duihaunButton.backgroundColor = LLWhiteColor;
        duihaunButton.layer.masksToBounds = YES;
        duihaunButton.layer.cornerRadius = 16;
        duihaunButton.frame = CGRectMake(LLScreenWidth-134, 175, 100, 32);
        [duihaunButton addTarget:self action:@selector(didSelecttype:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:duihaunButton];
        
        self.scoreLabel = scoreLabel;
        self.scoreLabel1 = scoreLabel1;
    }
    return _bgView;
}

- (void)didSelecttype:(LLButton *)button{
    //返回
    if (button.tag == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    //规则
    else if (button.tag == 1) {
        
    }
    //兑换
    else if (button.tag == 2) {
        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
